/**
 * Created by Administrator on 2016/11/15 0015.
 */
var CURRENT_URL = window.location.href;
var WROWSER_TYPE = getBrowserInfo();
var OS_TYPE = check_os();
var POST_URL = 'http://image.chinacar.com.cn/Ad/';
//var POST_URL = 'http://www.admin.org/Ad/';
var REFERRER = document.referrer;
var statIdName = "ad";
var ClassName = 'CHINACAR-KINGSTONE';
var AD_MARKS = 'adposition';
var AD_ID = 'ad_id';

$(function() {
    $('.'+ClassName).each(function() {
        var obj = $(this);
        $.ajax({
            url:POST_URL+'pv?callback=?',
            data: {
                adposition: obj.attr(AD_MARKS),
                ad_id: obj.attr(AD_ID),
                browser: WROWSER_TYPE,
                os: OS_TYPE,
                source_url: CURRENT_URL,
                referer: REFERRER,
                time: getTimestamp()
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function(data) {

            },
            error: function() {

            }
        });
    });

    $('.'+ClassName).click(function() {
        var obj = $(this);
        $.ajax({
            url:POST_URL+'click?callback=?',
            data: {
                adposition: obj.attr(AD_MARKS),
                ad_id: obj.attr(AD_ID),
                browser: WROWSER_TYPE,
                os: OS_TYPE,
                source_url: CURRENT_URL,
                referer: REFERRER,
                time: getTimestamp()
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function(data) {

            },
            error: function() {

            }
        });
    });


    $('.'+ClassName).mouseover(function() {
        var obj = $(this);
        $.ajax({
            url:POST_URL+'mouseover?callback=?',
            data: {
                adposition: obj.attr(AD_MARKS),
                ad_id: obj.attr(AD_ID),
                source_url: CURRENT_URL,
                time: getTimestamp()
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function(data) {

            },
            error: function() {

            }
        });
    });
});


function check_os() {
    windows = (navigator.userAgent.indexOf("Windows", 0) != -1) ? 1 : 0;
    mac = (navigator.userAgent.indexOf("mac", 0) != -1) ? 1 : 0;
    linux = (navigator.userAgent.indexOf("Linux", 0) != -1) ? 1 : 0;
    unix = (navigator.userAgent.indexOf("X11", 0) != -1) ? 1 : 0;
    if (windows) os_type = "MS Windows";
    else if (mac) os_type = "Apple mac";
    else if (linux) os_type = "Lunix";
    else if (unix) os_type = "Unix";
    else os_type = "MAC";
    return os_type;
}


var browser = getBrowserInfo();
var verinfo = (browser + "").replace(/[^0-9.]/ig, "");

function getBrowserInfo() {
    var agent = navigator.userAgent.toLowerCase();
    var regStr_ie = /msie [\d.]+;/gi;
    var regStr_ff = /firefox\/[\d.]+/gi
    var regStr_chrome = /chrome\/[\d.]+/gi;
    var regStr_saf = /safari\/[\d.]+/gi;
    var regStr_qq = /qqbrowser\/[\d.]+/gi;
    var browserName = 'other';

    if (agent.indexOf("msie") > 0) {
        browserName = 'ie';
    }

    if (agent.indexOf("trident") > 0 && agent.indexOf("rv") > 0) {
        browserName = 'ie';
    }

    if (agent.indexOf("firefox") > 0) {
        browserName = 'firefox';
    }


    if (agent.indexOf("qqbrowser") > 0) {
        browserName = 'qq';
    } else if (agent.indexOf("chrome") > 0) {
        browserName = 'chrome';
    }

    if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) {
        browserName = 'safari';
    }
    return browserName;
}


function getTimestamp() {
    var timestamp = new Date().getTime();
    return timestamp;
}


function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}



function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return unescape(arr[2]);
    else return null;
}



function genStatId() {
    var cookieId = getTimestamp();
    cookieId = Math.round(Math.random() * 3000000000);
    return cookieId;
}


function setStatId() {
    var cookieId = genStatId();
    setCookie(statIdName, cookieId);
}




function getStatId() {
    var statId = getCookie(statIdName);
    if (statId != null && statId.length > 0) {
        return statId;
    } else {
        setStatId();
        return getStatId();
    }
}


function getBrowerLanguage() {
    var lang = navigator.browserLanguage;
    return lang != null && lang.length > 0 ? lang : "";
}



function getPageTitle() {
    return document.title;
}